package com.itheima.bos.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr.King on 2017/3/21.
 */
@Controller
@Scope("prototype")
public class ProcessInstanceAction extends ActionSupport {


    //注入流程引擎
    @Autowired
    private ProcessEngine processEngine;

    //流程实例id
    private String id;
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 查询流程实例
     * @return
     */
    public String list(){
        ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
        List<ProcessInstance> list = query.list();
        ServletActionContext.getContext().getValueStack().set("list",list);
        return "list";
    }

    /**
     * 查询流程变量
     */
    public String findData() throws IOException {
        Map<String, Object> map = processEngine.getRuntimeService().getVariables(id);
        ServletActionContext.getResponse().setContentType("text/htlm;charset=utf-8");
        ServletActionContext.getResponse().getWriter().print(map);
        return NONE;
    }

    /**
     * 查询部署ID，Png图片名称，当前任务坐标
     */
    public String showPng() {
        //查寻流程实例
        ProcessInstance processInstance =
                processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(id).singleResult();
        //根据流程实例查寻对应的流程id
        String processDefinitionId = processInstance.getProcessDefinitionId();
        //根据流程id查询流程定义对象
        ProcessDefinitionEntity processDefinition =
                (ProcessDefinitionEntity) processEngine.getRepositoryService().getProcessDefinition(processDefinitionId);
        //根据流程对象获得图片名称
        String deploymentId = processDefinition.getDeploymentId();
        String diagramResourceName = processDefinition.getDiagramResourceName();
        //根据流程实例获取当前流程运行到那个任务节点
        String activityId = processInstance.getActivityId();
        ActivityImpl activityImpl = processDefinition.findActivity(activityId);
        //获取坐标
        int x = activityImpl.getX();
        int y = activityImpl.getY();
        int width = activityImpl.getWidth();
        int height = activityImpl.getHeight();

        ActionContext.getContext().getValueStack().set("deploymentId",deploymentId);
        ActionContext.getContext().getValueStack().set("imageName", diagramResourceName);
        ActionContext.getContext().getValueStack().set("x", x);
        ActionContext.getContext().getValueStack().set("y", y);
        ActionContext.getContext().getValueStack().set("width", width);
        ActionContext.getContext().getValueStack().set("height", height);
        return "showPng";
    }

    private String deploymentId;
    private String imageName;
    public void setDeploymentId(String deploymentId) {
        this.deploymentId = deploymentId;
    }
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public String viewImage(){
        InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId,imageName);
        ActionContext.getContext().getValueStack().set("pngStream",inputStream);
        return "viewImage";
    }
}
